package com.task.util;

import com.task.model.CommonParameter;
import com.task.model.TaskModel;
import javafx.util.Builder;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.*;

public class Dom4jReader {

    /**
     * @author carey
     * @see
     * @date 2018/2/10 下午9:14
     * @param
     * @return List<TaskModel>
     * @exception DocumentException
     */
    public List<TaskModel> readTaskXML() throws DocumentException {
        List<TaskModel> mapList =new ArrayList<>();

            String path = this.getClass().getResource("/").getPath();
            File file = new File(path + CommonParameter.TASKPATH);
             SAXReader reader=new SAXReader();
            //读取xml文件到Document中
            Document doc=reader.read(file);
            //获取xml文件的根节点
            Element rootElement=doc.getRootElement();
            //定义一个Element用于遍历
            Element fooElement;
            //遍历所有名叫“VALUE”的节点
            for(Iterator i = rootElement.elementIterator("task"); i.hasNext();){
                fooElement=(Element)i.next();
                Element crontab= fooElement.element("crontab");
                Element status= fooElement.element("status");
                TaskModel taskModel= new TaskModel.Builder()
                        .setClassPath(fooElement.attribute("class").getValue())
                        .setTaskName(fooElement.attribute("name").getValue())
                        .build();
                taskModel.setCrontab(crontab.getText());
                taskModel.setStatus(status.getTextTrim());
                mapList.add(taskModel);
            }

            return mapList;



    }

    public static void main(String[]  args){
        try {
            new  Dom4jReader().readTaskXML();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}

